﻿using System;
using System.Data;
using System.Text;
using MySql.Data.MySqlClient;
using Maticsoft.DBUtility;//Please add references
namespace Server.model
{
	/// <summary>
	/// 类security_account。
	/// </summary>
	[Serializable]
	public partial class Security_account
	{
		public Security_account()
		{}
		#region Model
		private int _id;
		private string _security_account;
		private int _user_id;
		/// <summary>
		/// auto_increment
		/// </summary>
		public int id
		{
			set{ _id=value;}
			get{return _id;}
		}
		/// <summary>
		/// 
		/// </summary>
		public string security_account
		{
			set{ _security_account=value;}
			get{return _security_account;}
		}
		/// <summary>
		/// 
		/// </summary>
		public int user_id
		{
			set{ _user_id=value;}
			get{return _user_id;}
		}
		#endregion Model


		#region  Method

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Security_account(int user_id, string security_accout)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select `id`,`security_account`,`user_id` ");
			strSql.Append(" FROM `security_account` ");
			strSql.Append(" where `user_id`=@user_id and `security_accout`=@security_accout ");
			MySqlParameter[] parameters = {
					new MySqlParameter("@id", MySqlDbType.Int32),
                    new MySqlParameter("@security_accout", MySqlDbType.VarChar, 100)};
			parameters[0].Value = id;
            parameters[1].Value = security_account;

			DataSet ds=DbHelperMySQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["id"]!=null && ds.Tables[0].Rows[0]["id"].ToString()!="")
				{
					this.id=int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
				}
				if(ds.Tables[0].Rows[0]["security_account"]!=null)
				{
					this.security_account=ds.Tables[0].Rows[0]["security_account"].ToString();
				}
				if(ds.Tables[0].Rows[0]["user_id"]!=null && ds.Tables[0].Rows[0]["user_id"].ToString()!="")
				{
					this.user_id=int.Parse(ds.Tables[0].Rows[0]["user_id"].ToString());
				}
			}
		}

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{

		return DbHelperMySQL.GetMaxID("`id`", "`security_account`"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from `security_account`");
			strSql.Append(" where `id`=@id ");

			MySqlParameter[] parameters = {
					new MySqlParameter("@id", MySqlDbType.Int32)};
			parameters[0].Value = id;

			return DbHelperMySQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add()
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into `security_account` (");
			strSql.Append("`security_account`,`user_id`)");
			strSql.Append(" values (");
			strSql.Append("@security_account,@user_id)");
			MySqlParameter[] parameters = {
					new MySqlParameter("@security_account", MySqlDbType.VarChar,100),
					new MySqlParameter("@user_id", MySqlDbType.Int32,11)};
			parameters[0].Value = security_account;
			parameters[1].Value = user_id;

            int line = DbHelperMySQL.ExecuteSql(strSql.ToString(),parameters);
            if (line > 0) return true;
            return false;
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update()
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update `security_account` set ");
			strSql.Append("`security_account`=@security_account");
			//strSql.Append("user_id=@user_id");
			strSql.Append(" where `id`=@id ");
			MySqlParameter[] parameters = {
					new MySqlParameter("@security_account", MySqlDbType.VarChar,100),
					//new MySqlParameter("@user_id", MySqlDbType.Int32,11),
					new MySqlParameter("@id", MySqlDbType.Int32,11)};
			parameters[0].Value = security_account;
			//parameters[1].Value = user_id;
			parameters[1].Value = id;

			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from `security_account` ");
			strSql.Append(" where id=@id ");
			MySqlParameter[] parameters = {
					new MySqlParameter("@id", MySqlDbType.Int32)};
			parameters[0].Value = id;

			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public void GetModel(int id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select `id`,`security_account`,`user_id` ");
			strSql.Append(" FROM `security_account` ");
			strSql.Append(" where `id`=@id ");
			MySqlParameter[] parameters = {
					new MySqlParameter("@id", MySqlDbType.Int32)};
			parameters[0].Value = id;

			DataSet ds=DbHelperMySQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["id"]!=null && ds.Tables[0].Rows[0]["id"].ToString()!="")
				{
					this.id=int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
				}
				if(ds.Tables[0].Rows[0]["security_account"]!=null )
				{
					this.security_account=ds.Tables[0].Rows[0]["security_account"].ToString();
				}
				if(ds.Tables[0].Rows[0]["user_id"]!=null && ds.Tables[0].Rows[0]["user_id"].ToString()!="")
				{
					this.user_id=int.Parse(ds.Tables[0].Rows[0]["user_id"].ToString());
				}
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select * ");
			strSql.Append(" FROM `security_account` ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperMySQL.Query(strSql.ToString());
		}

		#endregion  Method
	}
}

